import { useUserStore } from '@/store/modules/user';

// 权限检查
export function checkPermi(value: string[]) {
    if (value && value instanceof Array && value.length > 0) {
        const useUser = useUserStore();
        const all_permission = useUser.roles.some((item) => item.role === 'root');
        const permissions = useUser.permissions;
        const permissionDatas = value;

        if (all_permission) {
            return true;
        } else {
            const hasPermission = permissions.some((permission) => {
                return permissionDatas.includes(permission);
            });

            if (!hasPermission) {
                return false;
            }
            return true;
        }
    } else {
        console.error(`没有${value}权限`);
        return false;
    }
}

// 角色权限检查
export function checkRole(value: string[]) {
    if (value && value instanceof Array && value.length > 0) {
        const roles = useUserStore().roles;
        const permissionRoles = value;
        const root = roles.some((item) => item.role === 'root');

        if (root) {
            return true;
        } else {
            const hasRole = roles.some((role) => {
                return permissionRoles.includes(role.role) || permissionRoles.includes(role.description);
            });

            if (!hasRole) {
                return false;
            }
            return true;
        }
    } else {
        console.error(`没有${value}权限`);
        return false;
    }
}
